package test;

import java.nio.IntBuffer;

/**
 * 测试核心属性：position、limit、capacity
 */
public class IntBufferDemo {
    public static void main(String[] args) {
        // 分配8个长度
        IntBuffer intBuffer = IntBuffer.allocate(8);
        System.out.println("position = " + intBuffer.position() + "；limit = " + intBuffer.limit() + "；capacity = " + intBuffer.capacity());
        // 写入数据
        for (int i = 0; i < 5; i++) {
            int j = 2 * (i + 1);
            intBuffer.put(j);
            System.out.println("position = " + intBuffer.position() + "；limit = " + intBuffer.limit() + "；capacity = " + intBuffer.capacity());
        }
        // 切换到写模式
        intBuffer.flip();
        System.out.println("position = " + intBuffer.position() + "；limit = " + intBuffer.limit() + "；capacity = " + intBuffer.capacity());
        //
        while (intBuffer.hasRemaining()) {
            // 读取数据
            int j = intBuffer.get();
            System.out.print(j + " ");
        }
    }
}
